package com.fendaren.controller;

import com.fendaren.base.BaseInfoProperties;
import com.fendaren.grace.result.GraceJSONResult;
import com.fendaren.pojo.bo.CreateAdminBO;
import com.fendaren.pojo.bo.ResetPwdBO;
import com.fendaren.service.AdminService;
import com.fendaren.util.JWTUtils;
import com.fendaren.util.PagedGridResult;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("admininfo")
@Slf4j
public class AdminController extends BaseInfoProperties {

    @Autowired
    private AdminService adminService;

    @PostMapping("create")
    public GraceJSONResult create(@Valid @RequestBody CreateAdminBO createAdminBO) {

        adminService.createAdmin(createAdminBO);

        return GraceJSONResult.ok();
    }

    @PostMapping("list")
    public GraceJSONResult list(String accountName, Integer page, Integer limit) {

        if (page == null || page < 1) {
            page = 1;
        }
        if (limit == null || limit < 1) {
            limit = 10;
        }

        PagedGridResult listResult = adminService.getAdminList(accountName, page, limit);
        return GraceJSONResult.ok(listResult);
    }

    @PostMapping("delete")
    public GraceJSONResult delete(String username) {
        adminService.deleteAdmin(username);
        return GraceJSONResult.ok();
    }

    @PostMapping("resetPwd")
    public GraceJSONResult resetPwd(ResetPwdBO resetPwdBO) {

        resetPwdBO.modifyPwd();
        return GraceJSONResult.ok();
    }

}
